package com.yonyou.pmclouds.plan.processor;

import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.plan.entity.ScheduleVO;
import com.yonyou.pmclouds.plan.rmiitf.IScheduleQueryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @Description: 进度计划编制保存前处理类
 * @Author: zhangzhonghao
 * @Date: 2020-03-06
 *
 */

@Component
public class ScheduleUpdateBeforeProcessor implements SingleProcessor<ScheduleVO> {

    @Autowired
    private IScheduleQueryService scheduleQueryService;

    @Override
    public void process(ScheduleVO scheduleVO, ScheduleVO orig) throws BusinessException {
        ScheduleVO result = scheduleQueryService.queryById(scheduleVO.getPkSchedule());
        String creator = RuntimeEnvironment.getLoginUserId();

        if(result == null){
            throw new BusinessException("未找到该计划，请检查数据!");
        }
        if (result.getCreator() == null || !creator.equals(result.getCreator())) {
            throw new BusinessException("当前用户不是该根计划的创建人，不可操作！");
        }

        if(StringUtils.isEmpty(scheduleVO.getName())){
            throw new BusinessException("计划名称不允许为空！");
        }

        if(StringUtils.isBlank(scheduleVO.getPkProject())){
            throw new BusinessException("项目不能为空！");
        }
    }

}
